<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\Provider as ProviderModel;
use catcher\CatchAuth;
use think\facade\Db;
use catchAdmin\basics\request\ProviderCreateRequest;
/*
 * 客户信息
*/
class Provider extends CatchController
{
    
    protected $providerModel;
    
    /**
     *
     * @time 2022/03/08 10:30
     * @param ProviderModel $providerModel
     * @return mixed
     */
    public function __construct(ProviderModel $providerModel)
    {
        $this->providerModel = $providerModel;
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->providerModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @param ProviderCreateRequest $request
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,ProviderCreateRequest $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];

//        $params['account_code'] = strtoupper($params['account_code']);
        return CatchResponse::success($this->providerModel->storeBy($params),"新建成功");
    }

    /**
     *
     * @time 2022/02/28 16:55
     * @param ProviderCreateRequest $request
     * @return \think\response\Json
     */
    public function update(ProviderCreateRequest $request)
    {
        $params = $request->post();
        return CatchResponse::success($this->providerModel->updateBy($params['id'], $params),"修改成功");
    }

    /**
     * 删除
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->providerModel->deleteBy($id),'删除成功');
    }
    /**
     * 角色列表
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function part()
    {
        $where = [];
        $where[] = ['deleted_at','=',0];
        $data = DB::name('db_part')->field('id,part_name')->where($where)->select();
        return CatchResponse::success($data, '查询成功');
    }

    /**
     * 查询客户信息
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function getProvider(Request $request)
    {
        $role = $request->post('role');
        $plural = $request->post('plural');
        $where = [];
        $where[] = ['deleted_at','=',0];
        $sql='';
        if(!empty($plural)){
            $role = explode(',',$role);
            $where[] = ['role','like','%'.$role[0].'%'];
            $whereor=[];
            $whereor[] = ['deleted_at','=',0];
            $whereor[] = ['role','like','%'.$role[1].'%'];
            $data=Db::table('db_provider')
                ->field('id,account_code,account_name,role,relation_name,relation_phone')
                ->whereOr([$where,$whereor])
                ->select();
        }else{
            if(empty($role)){
                $data=Db::table('db_provider')
                    ->field('id,account_code,account_name,role,relation_name,relation_phone')
                    ->where($where)
                    ->select();
            }else{
                $where[] = ['role','like','%'.$role.'%'];
                $data=Db::table('db_provider')
                    ->field('id,account_code,account_name,role,relation_name,relation_phone')
                    ->where($where)
                    ->select();
            }
        }
//        $datas = [];
//       foreach ($data as $value)  {
//           $info = explode(',',$value['role']);
//           if(in_array($role,$info)){
//                 $datas[] = $value;
//           }
//       }
       return CatchResponse::success($data, '查询成功');
    }

    //供应商
    public function getSupplier(CatchAuth $auth){
        $user = $auth->user();
        $tenant_id = $user['tenant_id'];

        $where = [];
        $where[] = ['role','find in set',22];
        if($tenant_id != 0){
            $where[]=['tenant_id','=',$tenant_id];
        }

        $result = $this->providerModel->where($where)->select()->toArray();
        return CatchResponse::success($result,"供应商用户获取成功");
    }

    //制造商
    public function getManufacturer(CatchAuth $auth){
        $user = $auth->user();
        $tenant_id = $user['tenant_id'];

        $where = [];
        $where[] = ['role','find in set',23];
        if($tenant_id != 0){
            $where[]=['tenant_id','=',$tenant_id];
        }

        $result = $this->providerModel->where($where)->select()->toArray();
        return CatchResponse::success($result,"制造商用户获取成功");
    }
}